package com.itmk.upload;

import com.itmk.utils.AliOSSUtils;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@RestController
@RequestMapping("/api/upload")
public class ImageUploadController {
    /**
     * 文件上传
     */
    final AliOSSUtils aliOSSUtils;

    public ImageUploadController(AliOSSUtils aliOSSUtils) {
        this.aliOSSUtils = aliOSSUtils;
    }

    @CrossOrigin
    @RequestMapping("/uploadImage")
    public ResultVo upload(@RequestParam("file") MultipartFile file) throws IOException {  //参数名字和前端传输保持一致
        //file是一个临时文件，需要转存到指定位置，否则本次请求完成后临时文件会删除
        String s=aliOSSUtils.upload(file);
        log.info("图===="+s);
       // log.info(s);
        return ResultUtils.success( "成功",s);
    }

    /**
     * 文件下载到界面
     * @param name
     * @throws IOException
     */
    @RequestMapping("/downloadImage")
    public void download(String name, HttpServletResponse response) throws Exception {
        aliOSSUtils.downloadFileFromOSS(name,response);
     //   log.info("========");
        log.info("文件名字是{}",name);
    }

}
